﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using ImplementationInitialization;
using System.IO;
using LDSP.BRL;

namespace DataInitial
{
    public partial class InitialDataForm : Form
    {
        private ImportResult _importResult = new ImportResult();

        public InitialDataForm()
        {
            InitializeComponent();
        }

        #region UI events.
        private void btnBasicImport_Click(object sender, EventArgs e)
        {
            this.Enable(false);

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync(new bool[] { chkCatalog.Checked, chkCategory.Checked });
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            _importResult.ClearCodification();

            Import(this.txtCodificationDir.Text, this.txtCodificationFileDir.Text, this.txtSiteDir.Text, DocType.法律法规);
        }

        private void btnImportStandard_Click(object sender, EventArgs e)
        {
            _importResult.ClearStandard();

            Import(this.txtStandardDir.Text, this.txtStandardFileDir.Text, this.txtSiteDir.Text, DocType.标准);
        }

        private void btnImportAll_Click(object sender, EventArgs e)
        {
            _importResult.ClearAll();

            this.Enable(false);

            BackgroundWorker importAllWorker = new BackgroundWorker();
            importAllWorker.DoWork += new DoWorkEventHandler(importAllWorker_DoWork);
            importAllWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(importAllWorker_RunWorkerCompleted);
            importAllWorker.RunWorkerAsync(new object[] { chkCatalog.Checked, chkCategory.Checked, this.txtCodificationDir.Text, this.txtCodificationFileDir.Text, this.txtStandardDir.Text, this.txtStandardFileDir.Text, this.txtSiteDir.Text });
        }

        private void btnFolder_Click(object sender, EventArgs e)
        {
            this.txtCodificationDir.Text = ChooseDir();
        }

        private void btnStandardFolder_Click(object sender, EventArgs e)
        {
            DialogResult result = this.folderBrowserDialog1.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.txtStandardDir.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnCodificationFileDir_Click(object sender, EventArgs e)
        {
            this.txtCodificationFileDir.Text = ChooseDir();
        }

        private void btnStandardFileDir_Click(object sender, EventArgs e)
        {
            this.txtStandardFileDir.Text = ChooseDir();
        }

        private void btnSiteDir_Click(object sender, EventArgs e)
        {
            this.txtSiteDir.Text = ChooseDir();
        }

        private void btnImportReport_Click(object sender, EventArgs e)
        {
            new ImportReportForm(_importResult).ShowDialog();
        }
        #endregion

        #region private methods
        private string ChooseDir()
        {
            string path = string.Empty;
            DialogResult result = this.folderBrowserDialog1.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                path = this.folderBrowserDialog1.SelectedPath;
            }
            return path;
        }

        private void Import(string dir, string fileDir, string siteDir, DocType docType)
        {
            this.Enable(false);

            BackgroundWorker importWorker = new BackgroundWorker();
            importWorker.DoWork += new DoWorkEventHandler(importWorker_DoWork);
            importWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(importWorker_RunWorkerCompleted);
            importWorker.RunWorkerAsync(new object[] { dir, fileDir, siteDir, docType });
        }

        private void DoImport(string dir, string fileDir, string siteDir, DocType docType)
        {
            if (Directory.Exists(dir))
            {
                //设定import.
                FinalImport import = FinalImportFactory.GetFinalImport(docType);
                import.ImportResult = _importResult;

                //按文件导入.
                foreach (FileInfo file in new DirectoryInfo(dir).GetFiles())
                {
                    if (file.Extension == ".xls"
                        || file.Extension == ".xlsx")
                    {
                        import.Run(file.FullName);
                    }
                }

                //导入法规及标准附件.
                import.UpdateAttachment(fileDir, siteDir + "\\Docs");

                //刷新失效列表.
                import.RefreshDisableList();
            }
        }

        private void Enable(bool enable)
        {
            foreach (Control ctrl in this.Controls)
            {
                ctrl.Enabled = enable;
            }

            this.progressBar1.Visible = !enable;
        }
        #endregion

        #region background worker
        void importAllWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Enable(true);
        }

        void importAllWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = e.Argument as object[];
            bool catalog = (bool)args[0];
            bool category = (bool)args[1];
            string codificationDir = args[2] as string;
            string codificationFileDir = args[3] as string;
            string standardDir = args[4] as string;
            string standardFileDir = args[5] as string;
            string siteDir = args[6] as string;

            if (catalog == true)
            {
                CatologImport.Run();
            }

            if (category == true)
            {
                CategoryImport.Run();
            }

            if (Directory.Exists(codificationDir))
            {
                DoImport(codificationDir, codificationFileDir, siteDir, DocType.法律法规);
            }

            if (Directory.Exists(standardDir))
            {
                DoImport(standardDir, standardFileDir, siteDir, DocType.标准);
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Enable(true);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            bool[] args = e.Argument as bool[];
            bool catalog = args[0];
            bool category = args[1];

            if (catalog == true)
            {
                CatologImport.Run();
            }

            if (category == true)
            {
                CategoryImport.Run();
            }
        }

        void importWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Enable(true);
        }

        void importWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = e.Argument as object[];
            string dir = args[0] as string;
            string fileDir = args[1] as string;
            string siteDir = args[2] as string;
            DocType docType = (DocType)args[3];

            DoImport(dir, fileDir, siteDir, docType);
        }
        #endregion
    }
}
